package com.zeroup.matrix.download;

import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.zeroup.matrix.VediosActivity;
import com.zeroup.matrix.bean.MGanVideo;
import com.zeroup.matrix.utile.SQLiteHelper;
import com.zeroup.matrix.utile.SaveUtils;

public class AutoVedioDownLoad implements VedioDownLoad{

    VediosActivity activity;

    public AutoVedioDownLoad(VediosActivity activity) {
        this.activity = activity;
    }

    @Override
    public void download(MGanVideo mGanVideo) {
        new Thread(() -> new AndroidDownloadManager(activity, mGanVideo.getVideoThirdUrl())
                .setListener(new AndroidDownloadManagerListener() {
                    @Override
                    public void onPrepare() {
                        Log.d("downloadVideo", "onPrepare");
                    }

                    @Override
                    public void onSuccess(String path) {
//                        Toast.makeText(activity, "视频已保存到相册", Toast.LENGTH_SHORT).show();
                        SaveUtils.saveVideoToAlbum(activity,path);
//                        SaveUtils.saveVideoToAlbum()
                        Log.d("downloadVideo", "onSuccess >>>>" + path);
                        activity.autoVedioDownloadComd(mGanVideo);
                    }

                    @Override
                    public void onFailed(Throwable throwable) {
//                        Toast.makeText(activity, "视频下载失败，请重新下载！", Toast.LENGTH_SHORT).show();
                        Log.e("downloadVideo", "onFailed", throwable);
                    }
                }).download()).start();
    }
}
